package willevaluate.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import common.BaseActivity;
import utils.KeyBoard;
import utils.MyLogUtils;
import utils.ToastUtils;
import view.MyConfirmDialog;
import willevaluate.bean.ExpertEvaluateCollegeBean;
import willevaluate.bean.ExpertEvaluateCollegeDetailBean;

/**
 * Created by Administrator on 2017/5/22.
 * 3800/800/8800填高校
 */

@ContentView(R.layout.expert_evaluate_college_detail)
public class ExpertEvaluateCollegeDetail extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.common_title)
    private TextView common_title;

    @ViewInject(R.id.expert_evaluate_college_detail_school_name)
    private EditText expert_evaluate_college_detail_school_name;
    @ViewInject(R.id.expert_evaluate_college_detail_item1)
    private EditText expert_evaluate_college_detail_item1;
    @ViewInject(R.id.expert_evaluate_college_detail_item2)
    private EditText expert_evaluate_college_detail_item2;
    @ViewInject(R.id.expert_evaluate_college_detail_item3)
    private EditText expert_evaluate_college_detail_item3;
    @ViewInject(R.id.expert_evaluate_college_detail_item4)
    private EditText expert_evaluate_college_detail_item4;
    @ViewInject(R.id.expert_evaluate_college_detail_item5)
    private EditText expert_evaluate_college_detail_item5;
    @ViewInject(R.id.expert_evaluate_college_detail_item6)
    private EditText expert_evaluate_college_detail_item6;
    @ViewInject(R.id.expert_evaluate_college_detail_commit)
    private Button expert_evaluate_college_detail_commit;

    private int position;
    private ExpertEvaluateCollegeBean expertEvaluateCollegeBean;
    private String schoolName;
    private List<ExpertEvaluateCollegeDetailBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityController.addActivity(this);
        position = getIntent().getIntExtra("position", -1);
        expertEvaluateCollegeBean = (ExpertEvaluateCollegeBean) getIntent().getSerializableExtra("bean");
        if (expertEvaluateCollegeBean == null) {
            expertEvaluateCollegeBean = new ExpertEvaluateCollegeBean();
        }
        initView();
        KeyBoard.showKeyBoard();
    }

    private void initView() {
        common_back.setOnClickListener(this);
        common_title.setText("模拟填报志愿");
        expert_evaluate_college_detail_commit.setOnClickListener(this);

        schoolName = expertEvaluateCollegeBean.getCollegeName();
        list = expertEvaluateCollegeBean.getMajors();

        if (schoolName != null && !TextUtils.isEmpty(schoolName)) {
            expert_evaluate_college_detail_school_name.setText(schoolName);
            expert_evaluate_college_detail_school_name.setSelection(schoolName.length());
        }
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                int position = list.get(i).getPosition();
                switch (position) {
                    case 0:
                        expert_evaluate_college_detail_item1.setText(list.get(position).getName());
                        break;
                    case 1:
                        expert_evaluate_college_detail_item2.setText(list.get(position).getName());
                        break;
                    case 2:
                        expert_evaluate_college_detail_item3.setText(list.get(position).getName());
                        break;
                    case 3:
                        expert_evaluate_college_detail_item4.setText(list.get(position).getName());
                        break;
                    case 4:
                        expert_evaluate_college_detail_item5.setText(list.get(position).getName());
                        break;
                    case 5:
                        expert_evaluate_college_detail_item6.setText(list.get(position).getName());
                        break;
                    default:
                        break;
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                back();
                break;
            case R.id.expert_evaluate_college_detail_commit:
                commit();
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        back();
    }

    private void commit() {
        if (!TextUtils.isEmpty(expert_evaluate_college_detail_school_name.getText().toString())) {
            expertEvaluateCollegeBean.setCollegeName(expert_evaluate_college_detail_school_name.getText().toString());
        } else {
            ToastUtils.shortToast("高校名不能为空");
            return;
        }
        if (list != null) {
            list.clear();
        } else {
            list = new ArrayList<>();
        }
        if (!TextUtils.isEmpty(expert_evaluate_college_detail_item1.getText().toString())) {
            ExpertEvaluateCollegeDetailBean expertEvaluateCollegeDetailBean = new ExpertEvaluateCollegeDetailBean();
            expertEvaluateCollegeDetailBean.setName(expert_evaluate_college_detail_item1.getText().toString());
            expertEvaluateCollegeDetailBean.setPosition(0);
            list.add(expertEvaluateCollegeDetailBean);
        }
        if (!TextUtils.isEmpty(expert_evaluate_college_detail_item2.getText().toString())) {
            ExpertEvaluateCollegeDetailBean expertEvaluateCollegeDetailBean = new ExpertEvaluateCollegeDetailBean();
            expertEvaluateCollegeDetailBean.setName(expert_evaluate_college_detail_item2.getText().toString());
            expertEvaluateCollegeDetailBean.setPosition(1);
            list.add(expertEvaluateCollegeDetailBean);
        }
        if (!TextUtils.isEmpty(expert_evaluate_college_detail_item3.getText().toString())) {
            ExpertEvaluateCollegeDetailBean expertEvaluateCollegeDetailBean = new ExpertEvaluateCollegeDetailBean();
            expertEvaluateCollegeDetailBean.setName(expert_evaluate_college_detail_item3.getText().toString());
            expertEvaluateCollegeDetailBean.setPosition(2);
            list.add(expertEvaluateCollegeDetailBean);
        }
        if (!TextUtils.isEmpty(expert_evaluate_college_detail_item4.getText().toString())) {
            ExpertEvaluateCollegeDetailBean expertEvaluateCollegeDetailBean = new ExpertEvaluateCollegeDetailBean();
            expertEvaluateCollegeDetailBean.setName(expert_evaluate_college_detail_item4.getText().toString());
            expertEvaluateCollegeDetailBean.setPosition(3);
            list.add(expertEvaluateCollegeDetailBean);
        }
        if (!TextUtils.isEmpty(expert_evaluate_college_detail_item5.getText().toString())) {
            ExpertEvaluateCollegeDetailBean expertEvaluateCollegeDetailBean = new ExpertEvaluateCollegeDetailBean();
            expertEvaluateCollegeDetailBean.setName(expert_evaluate_college_detail_item5.getText().toString());
            expertEvaluateCollegeDetailBean.setPosition(4);
            list.add(expertEvaluateCollegeDetailBean);
        }
        if (!TextUtils.isEmpty(expert_evaluate_college_detail_item6.getText().toString())) {
            ExpertEvaluateCollegeDetailBean expertEvaluateCollegeDetailBean = new ExpertEvaluateCollegeDetailBean();
            expertEvaluateCollegeDetailBean.setName(expert_evaluate_college_detail_item6.getText().toString());
            expertEvaluateCollegeDetailBean.setPosition(5);
            list.add(expertEvaluateCollegeDetailBean);
        }
        expertEvaluateCollegeBean.setMajors(list);
        Intent i = new Intent();
        i.putExtra("position", position);
        i.putExtra("bean", expertEvaluateCollegeBean);
        setResult(2, i);
        this.finish();
        this.overridePendingTransition(0, R.anim.zoomout);
    }

    private void back() {
        MyConfirmDialog myConfirmDialog = new MyConfirmDialog(ExpertEvaluateCollegeDetail.this, "是否保存?", "保存", "不保存");
        myConfirmDialog.show();
        myConfirmDialog.setConfirmListener(() -> commit());
        myConfirmDialog.setCancelListener(() -> ExpertEvaluateCollegeDetail.this.finish());
    }

}
